package com.accenture.portal.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.accenture.portal.controller.ArticleController;
import com.accenture.portal.dao.ArticleMapper;
import com.accenture.portal.dao.UserArticleMapper;
import com.accenture.portal.model.Article;
import com.accenture.portal.model.UserArticle;
import com.accenture.portal.service.ArticleService;

@Service
public class ArticleServiceImpl implements ArticleService {

	protected static final Logger LOG = Logger
			.getLogger(ArticleServiceImpl.class);

	@Autowired
	@Resource(name = "articleMapperImpl")
	ArticleMapper articleMapper;

	@Autowired
	@Resource(name = "userArticleMapperImpl")
	UserArticleMapper userArticleMapper;

	@Override
	public List<Article> getUnreadArticle(Integer user_id, int i) {
		int zer = 0;
		return articleMapper.getUnreadArticle(user_id, zer, i);
	}

	@Override
	public List<Article> getUnreadList(Integer user_id, Integer start,
			Integer end) {
		return articleMapper.getUnreadList(user_id, start, end);
	}

	@Override
	public int getUnreadCount(int user_id) {
		return articleMapper.getUnreadCount(user_id);
	}

	@Override
	public List<Article> getAllArticle() {
		return articleMapper.getAllArticle();
	}

	@Override
	public Article getContent(Integer article_id) {
		return articleMapper.getContent(article_id);
	}

	@Override
	public void addArticle(Article article) {
		articleMapper.addArticle(article);
	}

	@Override
	public void deleteArticle(Integer article_id) {
		articleMapper.deleteArticle(article_id);
	}

	@Override
	public void editArticle(Article article) {
		/*String oldContent=articleMapper.getArticleById(article.getArticle_id()).getContent();
		System.out.print("oldContent:"+oldContent+"==============================================");*/
		/*articleMapper.editArticleStart(article);
		//String NewContent=articleMapper.getArticleById(article.getArticle_id()).getContent();
		System.out.print("delete success==============================================");
		articleMapper.editArticleThen(article);*/
		articleMapper.editArticle(article);
	}

	@Override
	public void markAsReadMulti(Integer user_id, String[] markMulti) {
		for (Integer i = 0; i < markMulti.length; i++) {
			Integer article_id = Integer.valueOf(markMulti[i]);
			UserArticle userArticle = new UserArticle();
			userArticle.setUser_id(user_id);
			userArticle.setArticle_id(article_id);
			List<Article> article = userArticleMapper
					.getUserArticle(userArticle);
			if (article.size() == 0) {
				userArticleMapper.markAsRead(userArticle);
			}
		}
	}

	@Override
	public int getTotalCount() {
		return articleMapper.getTotalCount();
	}

	@Override
	public List<Article> getAllByPage(int start, int end) {
		return articleMapper.getAllByPage(start, end);
	}

	@Override
	public Article getArticleById(Integer article_id) {
		return articleMapper.getArticleById(article_id);
	}

}
